home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / pcr / pcr4_4.lha / DIST / threads / ThreadsTestUtils.c < prev    next >
C/C++ Source or Header  |  1989-11-30  |  5KB  |  175 lines

  1. /* begincopyright
  2.   Copyright (c) 1988 Xerox Corporation. All rights reserved.
  3.   Use and copying of this software and preparation of derivative works based
  4.   upon this software are permitted. Any distribution of this software or
  5.   derivative works must comply with all applicable United States export
  6.   control laws. This software is made available AS IS, and Xerox Corporation
  7.   makes no warranty about the software, its performance or its conformity to
  8.   any specification. Any person obtaining a copy of this software is requested
  9.   to send their name and post office or electronic mail address to:
  10.     PCR Coordinator
  11.     Xerox PARC
  12.     3333 Coyote Hill Rd.
  13.     Palo Alto, CA 94304
  14.   endcopyright */
  15.  
  16. /*
  17.  * ThreadsTestUtils.c
  18.  *
  19.  * Demers, November 30, 1989 5:38:41 pm PST
  20.  *
  21.  * Basic regression tests for threads
  22.  */
  23.  
  24. #include "xr/BasicTypes.h"
  25. #include "xr/Threads.h"
  26. #include "xr/ThreadsBackdoor.h"
  27. #include "xr/ThreadsPrivate.h"
  28. #include "xr/ThreadsStatsPrivate.h"
  29. #include "xr/ThreadsMsgPrivate.h"
  30. #include "xr/UIO.h"
  31. #include "xr/ThreadsSignalsPrivate.h"
  32. #include "xr/Errno.h"
  33.  
  34. #include <sys/types.h>
  35. #include <netinet/in.h>
  36. #include <sys/socket.h>
  37. #include <sys/stat.h>
  38. #include <sys/file.h>
  39. #include <sys/time.h>
  40.  
  41. #include <netinet/in.h>
  42.  
  43. void (*XR_testFailedProc)() = NIL;
  44.  
  45. void
  46. XR_TestFailed(msg)
  47.     char *msg;
  48. {
  49.     if( msg == NIL ) msg = "(no message)";
  50.     XR_ConsoleMsg("\n\nTEST FAILED: %s\n\n", msg);
  51.     if( XR_testFailedProc != NIL ) {
  52.         (*XR_testFailedProc)(msg);
  53.     } else {
  54.         XR_CallDebugger();
  55.     }
  56.     for(;;) XR_SpinStep(100000);
  57.     /*NOTREACHED*/
  58. }
  59.  
  60. void
  61. PutInterval(from, to)
  62.     struct timeval *from;
  63.     struct timeval *to;
  64. {
  65.     int sec, msec, usec;
  66.     sec = to->tv_sec - from->tv_sec;
  67.     usec = to->tv_usec - from->tv_usec;
  68.     if( usec < 0 ) {
  69.         usec += 1000000;
  70.         sec -= 1;
  71.     }
  72.     msec = ((usec+500)/1000);
  73.     XR_ConsoleMsg("%d.%03d", sec, msec);
  74. }
  75.  
  76. void
  77. DoATest(testName, testProc)
  78.     char *testName;
  79.     void (*testProc)();
  80. {
  81.     static struct itimerval itvOn = { {0, 0}, {36000, 0} };
  82.     static struct itimerval itvOff = { {0, 0}, {0, 0} };
  83.     struct timeval realStart, realEnd;
  84.     struct itimerval itvDone;
  85. #   if XR_STATS
  86.         int nsw, nswi;
  87. #   endif
  88.  
  89.     XR_ConsoleMsg("\nStarting test of %s ...\n\n", testName);
  90.     (void)gettimeofday(&realStart, 0);
  91.     if( XR_sysArea->sa_numVP == 1 ) {
  92.     (void)setitimer(ITIMER_PROF, /*val=*/ &itvOn, /*oval=*/ 0);
  93.     }
  94. #   if XR_STATS
  95.     nsw = XR_sysArea->sa_stats[XR_STATS_Switch];
  96.     nswi = XR_sysArea->sa_stats[XR_STATS_SwitchIdle];
  97.     (*testProc)();
  98.     nsw = XR_sysArea->sa_stats[XR_STATS_Switch] - nsw;
  99.     nswi = XR_sysArea->sa_stats[XR_STATS_SwitchIdle] - nswi;
  100. #   else
  101.     (*testProc)();
  102. #   endif
  103.     (void)gettimeofday(&realEnd, 0);
  104.     if( XR_sysArea->sa_numVP == 1 ) {
  105.     (void)setitimer(ITIMER_PROF, /*val=*/ &itvOff, /*oval=*/ &itvDone);
  106.     }
  107.     XR_ConsoleMsg("\n... done test of %s.\nTimes: real ", testName);
  108.     PutInterval(&realStart, &realEnd);
  109.     if( XR_sysArea->sa_numVP == 1 ) {
  110.     XR_ConsoleMsg(", cpu ");
  111.     PutInterval(&itvDone.it_value, &itvOn.it_value);
  112.     }
  113. #   if XR_STATS
  114.     XR_ConsoleMsg(".\nThread switches: %d, idle: %d", nsw, nswi);
  115. #   endif
  116.     XR_ConsoleMsg(".\n\n");
  117. }
  118.  
  119. /*
  120.  * Utility random number generator for tests ...
  121.  *  ... stolen from Unix library rand.c
  122.  */
  123.  
  124. static struct XR_MLRep randLock;
  125.  
  126. static unsigned long rand_Y[] = {
  127.  0x8ca0df45, 0x37334f23, 0x4a5901d2, 0xaeede075, 0xd84bd3cf,
  128.  0xa1ce3350, 0x35074a8f, 0xfd4e6da0, 0xe2c22e6f, 0x045de97e,
  129.  0x0e6d45b9, 0x201624a2, 0x01e10dca, 0x2810aef2, 0xea0be721,
  130.  0x3a3781e4, 0xa3602009, 0xd2ffcf69, 0xff7102e9, 0x36fab972,
  131.  0x5c3650ff, 0x8cd44c9c, 0x25a4a676, 0xbd6385ce, 0xcd55c306,
  132.  0xec8a31f5, 0xa87b24ce, 0x1e025786, 0x53d713c9, 0xb29d308f,
  133.  0x0dc6cf3f, 0xf11139c9, 0x3afb3780, 0x0ed6b24c, 0xef04c8fe,
  134.  0xab53d825, 0x3ca69893, 0x35460fb1, 0x058ead73, 0x0b567c59,
  135.  0xfdddca3f, 0x6317e77d, 0xaa5febe5, 0x655f73e2, 0xd42455bb,
  136.  0xe845a8bb, 0x351e4a67, 0xa36a9dfb, 0x3e0ac91d, 0xbaa0de01,
  137.  0xec60dc66, 0xdb29309e, 0xcfa52971, 0x1f3eddaf, 0xe14aae61,
  138.  };
  139.  
  140. static long rand_j=23, rand_k=54;
  141.  
  142.  
  143. unsigned
  144. RandomInRange(range)
  145.     unsigned range;
  146. {
  147.     unsigned long m;
  148.     XR_MonitorEntry(&randLock);
  149.     m = rand_Y[rand_j]+rand_Y[rand_k];
  150.     rand_Y[rand_k] = m;
  151.     rand_j = rand_j - 1;
  152.     rand_k = rand_k - 1;
  153.     if(rand_j<0) rand_j = 54;
  154.     if(rand_k<0) rand_k = 54;
  155.     XR_MonitorExit(&randLock);
  156.     return ((m >> 5) & 0x7ffffff) % range;
  157. }
  158.  
  159.  
  160. bool
  161. CoinToss()
  162. {
  163.     return (RandomInRange(2) < 1);
  164. }
  165.  
  166.  
  167. bool
  168. CoinToss2(i, n)
  169.     unsigned i, n;
  170. {
  171.     return (RandomInRange(n) < i);
  172. }
  173.  
  174.  
  175.